﻿#pragma once
#ifndef _APP_H_
#define _APP_H_

#include <cinatra.hpp>
#include <iostream>
#include <cstring>
#include <cassert>

#include <thread>
#include <string>
#include <mutex>

#include "conf/conf.hpp"
#include "asio_timer.hpp"


using namespace std;
using namespace cinatra;


#ifdef WIN32
#define PATH_SPLIT "\\"
#else
#define PATH_SPLIT "/"

#endif // 

namespace bz 
{


class app 
{
public:
	static app* instance()
	{
		static app* single = new app();
		return single;
	}

	static string getAppDir()
	{
		return instance()->app_dir_;
	}
	static string getHomeDir()
	{
		return instance()->home_dir_;
	}

	void init(string home_path, string app_path, std::set<string> ip_set, std::function<void(int& port)> cb);

	int get_port() {
		return http_port_;
	}

	shared_ptr<AsioTimer>  timer_;
	std::function<void(int& port)> on_inited_ = nullptr;
	std::set<string> local_ip_set_;

private:
	app();
	void http_start();
	void http_run();

	std::once_flag flag_;
	string app_dir_;
	string home_dir_;
	shared_ptr<coro_http_server> http_server_;

	shared_ptr<std::thread> http_thread_;
	int http_port_;

	
};
}
#endif // _APP_H_
